package edu.xust.web.controller;

import edu.xust.config.Controller;
import edu.xust.config.RequestMapping;
import edu.xust.config.ResponseBody;
import edu.xust.dto.BaseDto;
import edu.xust.utils.UploadUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller("/uploadController")
public class UploadController {

    @RequestMapping("/upload")
    @ResponseBody
    public BaseDto  upload(HttpServletRequest request) {
        BaseDto dto = new BaseDto(405, "");
        boolean flag = true;
        String imageNames = "";
        Map<String, Map<String, Object>> returnMap = new UploadUtil().upload(request);
        String message = returnMap.keySet().iterator().next();
        Map<String, Object> resultMap;
        switch (message){
            case "success":
                resultMap = returnMap.get("success");
                dto.setCode(200);
                dto.setMsg("上传成功");
                dto.settList((List<String>)resultMap.get("fileNameList"));
                break;
            case "error":
                dto.setMsg("上传失败，请选择文件");
                break;
            case "exception":
                resultMap = returnMap.get("exception");
                dto.setCode(Integer.parseInt(resultMap.get("code").toString()));
                dto.setMsg(resultMap.get("msg").toString());
                dto.setObj(resultMap.get("exception"));
                break;
            default:
                break;
        }

        return dto;

    }
}
